<?php
/**
 * @file fashiontalkdefine.php
 * @author CaoQiCheng
 * @date 2016年5月26日 下午17:16:11
 * @brief 
 */

namespace haibao\cms\data\fashiontalk;

class FashionTalkDefine extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\fashiontalk\FashionTalkDefine');
    }
    
    
    public function getMonths(){
    	$data = array();
    	$sql = <<<EOF
	select DISTINCT(DATE_FORMAT(PublishTime,'%Y-%m')) as monthStr
	from FashionTalkDefine
	where Status = 1 and PublishTime != '0000-00-00 00:00:00' and Published=1
	order by monthStr desc
EOF;
    	$result = $this->query($sql);
    	while ($result->fetch_assoc()){
    		array_push($data,$result->getData('monthStr'));
    	}
    	return $data;
    }
    
    public function updateClickNum($id){
    	$sql = "update FashionTalkProperty set ClickNum=ClickNum+1 where FashionTalkId=%s";
    	$result  = $this->query($sql,array($id));
    }
    
    /**
     * 保存专访信息
     */
    public function saveFashiontalkInfo($model,$fashiontalkImageArr,$fashiontalkInterviewArr,$fashiontalkProperty){
    	$this->beginTranscation();
    	try {
    		if($model->Id){
    			$this->save($model);
    		}else {
    			$this->add($model);
    			$this->saveFashiontalkProperty($model->Id, $fashiontalkProperty);
    		}
    		$this->saveFashiontalkImage($model->Id, $fashiontalkImageArr);
    		$this->saveFashiontalkInterview($model->Id, $fashiontalkInterviewArr);
    	
    	} catch (\Exception $e) {
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    }
    /**
     * 保存专访对应的图片
     */
    public function saveFashiontalkImage($fashiontalkId,$fashiontalkImageArr){
    	if(!$fashiontalkId){
    		return false;
    	}
    	$this->query('delete from `FashionTalkImage` where FashionTalkId = %s', array(
    			$fashiontalkId
    	));
    	if(isset($fashiontalkImageArr) && $fashiontalkImageArr){
    		foreach($fashiontalkImageArr as $key=>$val){
    			$val->FashionTalkId = $fashiontalkId;
    		}
    		$this->addAll($fashiontalkImageArr);
    	}
    }
    /**
     * 保存专访对应的问题
     */
    public function saveFashiontalkInterview($fashiontalkId,$fashiontalkInterviewArr){
    	if(!$fashiontalkId){
    		return false;
    	}
    	$this->query('delete from `FashionTalkInterview` where FashionTalkId = %s', array(
    			$fashiontalkId
    	));
    	if(isset($fashiontalkInterviewArr) && $fashiontalkInterviewArr){
    		foreach($fashiontalkInterviewArr as $key=>$val){
    			$val->FashionTalkId = $fashiontalkId;
    		}
    		$this->addAll($fashiontalkInterviewArr);
    	}
    }
    /**
     * 保存第一次的专访点击
     */
    public function saveFashiontalkProperty($fashiontalkId,$fashiontalkProperty){
    	if(!$fashiontalkId){
    		return false;
    	}
    	if($fashiontalkProperty){
    		$fashiontalkProperty->FashionTalkId = $fashiontalkId;
    		$this->add($fashiontalkProperty);
    	}
    }
    
    /**
     * 删除专题
     */
    public function delFashiontalkById($fashiontalkId){
    	$sql = "update FashionTalkDefine set Status = '".\haibao\cms\model\data\fashiontalk\FashionTalkDefine::STATUS_DEL."' where Id =%s";
    	$this->query($sql,array($fashiontalkId));
    }
    
    
    /**
     * 判断AccessUrl是否重复
     */
    public function getAccessUrlIsExist($AccessUrl,$fashionTalkId){
    	$checkAnd = $fashionTalkId ? "and Id<>".$fashionTalkId : "";
    	$sql = "select Id,AccessUrl from FashionTalkDefine where Status=1 and AccessUrl=%s ".$checkAnd." limit 1";
    	$result  = $this->query($sql,array($AccessUrl))->fetch_assoc();
    	return $result;
    }
    
    
    
    
}